import store from '@/store/index.js'
exports.install = function(Vue, options) {
	// 全局函数

	Number.prototype.toFixed = function(d) {
		var s = this + "";
		if (!d) d = 0;
		if (s.indexOf(".") == -1) s += ".";
		s += new Array(d + 1).join("0");
		if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (d + 1) + "})?)\\d*$").test(s)) {
			var s = "0" + RegExp.$2,
				pm = RegExp.$1,
				a = RegExp.$3.length,
				b = true;
			if (a == d + 2) {
				a = s.match(/\d/g);
				if (parseInt(a[a.length - 1]) > 4) {
					for (var i = a.length - 2; i >= 0; i--) {
						a[i] = parseInt(a[i]) + 1;
						if (a[i] == 10) {
							a[i] = 0;
							b = i != 1;
						} else break;
					}
				}
				s = a.join("").replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"), "$1.$2");
			}
			if (b) s = s.substr(1);
			return (pm + s).replace(/\.$/, "");
		}
		return this + "";
	};

	/**
	 * modal
	 */
	Vue.prototype.$showModal = function(param) {
		let title = typeof param.title === 'undefined' ? '' : param.title;
		let contentColor = param.contentColor || '';
		let showCancel = typeof param.showCancel != 'undefined' ? param.showCancel : true;
		let cancelText = param.cancelText || '取消';
		let cancelColor = param.cancelColor || '#696969';
		let confirmText = param.confirmText || '确定';
		let confirmColor = param.confirmColor || '#0E932E';
		if (showCancel) {
			var buttons = param.buttons || [{
					title: cancelText,
					titleColor: cancelColor
				},
				{
					title: confirmText,
					titleColor: confirmColor
				}
			];
		} else {
			// 没有取消按钮
			var buttons = param.buttons || [{
				title: confirmText,
				titleColor: confirmColor
			}];
		}

		//#ifdef APP-PLUS
		uni.$off('_systemShowModalMessage');
		uni.setStorageSync('_systemShowModalParam', {
			title: title,
			content: param.content,
			contentColor: contentColor,
			buttons: buttons
		});
		this.$jump('$modal');
		uni.$once('_systemShowModalMessage', (res) => {
			if (typeof param.success != 'undefined') {
				param.success(res);
			}
		});
		//#endif

		//#ifndef APP-PLUS
		uni.showModal({
			title: title,
			content: param.content,
			showCancel: showCancel,
			cancelText: cancelText,
			cancelColor: cancelColor,
			confirmText: confirmText,
			confirmColor: confirmColor,
			success: (res) => {
				if (typeof param.success != 'undefined') {
					param.success(res);
				}
			}
		});
		//#endif
	};

	/**
	 * 跳转页面
	 */
	Vue.prototype.$jump = function(page, redirect) {
		// 是否关闭当前页面
		redirect = typeof redirect != 'undefined' ? redirect : false;
		if (redirect) {
			uni.redirectTo({
				url: page
			});
		} else {
			if (page.indexOf('$modal') == 0) {
				// 模拟对话框弹出动画
				uni.navigateTo({
					url: '/pages/modal/modal',
					animationType: "fade-in"
				});
			} else {
				uni.navigateTo({
					url: page,
					animationType: "zoom-fade-out"
				});
			}
		}
	};


	Vue.prototype.$mustLogin = () => {
		return new Promise((resolve, reject) => {
			if (!uni.getStorageSync('AppToken')) {
				Vue.prototype.$showModal({
					title: '请确认',
					content: '您还未登录，是否立即登录？',
					confirmText: '去登录',
					confirmColor: '#0E932E',
					success(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/login/login'
							})
						}
					}
				});
			} else {
				resolve(true)
			}
		})
	}

};